package com.kelvin.paopao.module.person.tagset;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.kelvin.paopao.R;
import com.kelvin.paopao.adapter.SetTagAdapter;
import com.kelvin.paopao.api.responseBean.TagModel;
import com.kelvin.paopao.injector.components.DaggerSetTagComponent;
import com.kelvin.paopao.injector.modules.SetTagModule;
import com.kelvin.paopao.module.base.BaseActivity;
import com.kelvin.paopao.widgets.CustomToolbar;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/6/13.
 */

public class SetTagActivity extends BaseActivity<SetTagPresenter> implements ISetTagView {
    @BindView(R.id.custom_toolbar)
    CustomToolbar customToolbar;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    private String setTag;

    private LinearLayoutManager manager;
    @Inject
    SetTagAdapter adapter;
    private List<String> tagArray;


    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_set_tag;
    }

    @Override
    protected void initInjector() {
        DaggerSetTagComponent.builder().setTagModule(new SetTagModule(this))
                .build().inject(this);
    }

    @Override
    protected void initViews() {
        setTag = getIntent().getStringExtra("setTag");
        getTagArray(setTag);

        customToolbar.showBackIcon(this);
        customToolbar.inflateMenu(R.menu.menu_person_detail_confirm);
        customToolbar.setMenuOnClick(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                String result = getSelected(adapter.getData());
                Intent intent = new Intent();
                intent.putExtra("result", result);
                setResult(Activity.RESULT_OK, intent);
                finish();
                return true;
            }
        });
        manager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(manager);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                List<TagModel> models = adapter.getData();
                models.get(position).setSelected(!models.get(position).isSelected());
                adapter.notifyDataSetChanged();
            }
        });
        recyclerView.setAdapter(adapter);
    }

    private void getTagArray(String setTag) {
        String[] tags = setTag.split("\\|");
        tagArray = new ArrayList<>();
        for (String str : tags) {
            tagArray.add(str);
        }
    }

    /**
     * 获取选择的标签
     *
     * @param data
     */
    private String getSelected(List<TagModel> data) {
        List<String> seletedStr = new ArrayList<>();
        for (TagModel model : data) {
            if (model.isSelected()) {
                seletedStr.add(model.getLbName());
            }
        }
        StringBuilder result = new StringBuilder();
        for (String str : seletedStr) {
            result.append(str + "|");
        }
        return result.toString().substring(0, result.length() - 1);

    }

    @Override
    protected void updateViews(boolean isRefresh) {
        mPresenter.getData(isRefresh);
    }


    @Override
    public void loadData(List<TagModel> models) {
        for (TagModel model : models) {
            if (tagArray.contains(model.getLbName())) {
                model.setSelected(true);
            } else {
                model.setSelected(false);
            }
        }
        adapter.setNewData(models);
    }

    @Override
    public void showNoData() {
        View nodataView = LayoutInflater.from(this).inflate(R.layout.view_no_data,
                (ViewGroup) recyclerView.getParent(), false);
        adapter.setEmptyView(nodataView);
    }
}
